package com.nhatth.contactsharer;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.provider.ContactsContract.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AlphabetIndexer;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.SectionIndexer;

public class ContactAdapter extends ArrayAdapter<Contact> implements SectionIndexer{

	AlphabetIndexer indexer;
	Cursor c;
	
	public ContactAdapter(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if(v == null){
			LayoutInflater li = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = li.inflate(android.R.layout.simple_list_item_multiple_choice, null);
		}
		CheckedTextView ctv = (CheckedTextView)v.findViewById(android.R.id.text1);
		Contact con = getItem(position);
		ctv.setChecked(con.checked);
		ctv.setText(con.toString());
		if(con.checked) {
			ctv.setBackgroundColor(Color.GRAY);
		}
		else {
			ctv.setBackgroundColor(Color.TRANSPARENT);
		}
		return v;
	}

	public int getPositionForSection(int section) {
		return indexer.getPositionForSection(section);
	}

	public int getSectionForPosition(int position) {
		return indexer.getSectionForPosition(position);
	}

	public Object[] getSections() {
		return indexer.getSections();
	}
	
	public void setCursor(Cursor c)
	{
		this.c = c;
		indexer = new AlphabetIndexer(c, c.getColumnIndex(Contacts.DISPLAY_NAME), "#ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	}
}
